home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / beans / simplebean / example / Acme09Bean.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  5.8 KB  |  225 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. package acme.beans;
  18.  
  19. import java.awt.*;
  20. import java.awt.event.*;
  21. import java.beans.*;
  22. import java.io.Serializable;
  23. import java.util.Vector;
  24.  
  25.  
  26. public class Acme09Bean extends Canvas implements Serializable {
  27.  
  28.  
  29.     public Acme09Bean() {
  30.         this("Acme Bean Counter:");
  31.     }
  32.  
  33.     public Acme09Bean(String label) {
  34.         super();
  35.         // this.label = label + "X " + count;
  36.         updateLabel();
  37.         setFont(new Font("Dialog", Font.PLAIN, 12));
  38.     }
  39.  
  40.     public synchronized void paint(Graphics g) {
  41.  
  42.         int width = getSize().width;
  43.         int height = getSize().height;
  44.  
  45.         g.setColor(beanColor);
  46.         g.fillRect(1, 1, width - 2, height - 2);
  47.         g.draw3DRect(0, 0, width - 1, height - 1, !down);
  48.  
  49.         g.setColor(getForeground());
  50.         g.setFont(getFont());
  51.  
  52.         g.drawRect(2, 2, width - 4, height - 4);
  53.  
  54.         FontMetrics fm = g.getFontMetrics();
  55.         g.drawString(label, (width - fm.stringWidth(label)) / 2, 
  56.                           (height + fm.getMaxAscent() - fm.getMaxDescent()) / 2);
  57.     }
  58.  
  59.     public void updateLabel() {
  60.       //label = label + " " + count;
  61.         label = (new Integer(count)).toString();
  62.     }
  63.  
  64.     public void fireAction() {
  65.         if (debug) {
  66.           //System.err.println("Button " + getLabel() + " pressed.");
  67.             System.err.println("Button " + label + " pressed.");
  68.         }
  69.         Vector targets;
  70.         synchronized (this) {
  71.             targets = (Vector) listeners.clone();
  72.         }
  73.         ActionEvent actionEvt = new ActionEvent(this, 0, null);
  74.         for (int i = 0; i < targets.size(); i++) {
  75.             ActionListener target = (ActionListener)targets.elementAt(i);
  76.             target.actionPerformed(actionEvt);
  77.         }
  78.  
  79.         Component parent = getParent();
  80.         if (parent != null) {
  81.             parent.postEvent(new Event(this, Event.MOUSE_DOWN, null));
  82.         }
  83.     }
  84.  
  85.     public void setDebug(boolean x) {
  86.         boolean old = debug;
  87.         debug = x;
  88.     }
  89.  
  90.     public boolean getDebug() {
  91.         return debug;
  92.     }
  93.  
  94.     public Color getColor() {
  95.         return beanColor;
  96.     }
  97.  
  98.     public void setColor(Color newColor) {
  99.         beanColor = newColor;
  100.         repaint();
  101.     }
  102.   /*
  103.    * Tue Dec 17 21:24:45 1996
  104.    *
  105.    * Remove label property
  106.    * Still used in Bean, just can't
  107.    * set it externally, either by method call
  108.    * or Builder tool event hookup.
  109.     public String getLabel() {
  110.         return label;
  111.     }
  112.  
  113.     public void setLabel(String newLabel) {
  114.         String oldLabel = label;
  115.         label = newLabel;
  116.         sizeToFit();
  117.     }
  118.     */
  119.     public int getCount() {
  120.         return count;
  121.     }
  122.  
  123.     public void setCount(int newCount) {
  124.         int oldCount = count;
  125.         count = newCount;
  126.         sizeToFit();
  127.     }
  128.  
  129.     public Dimension getPreferredSize() {
  130.         FontMetrics fm = getFontMetrics(getFont());
  131.         return new Dimension(fm.stringWidth(label) + TEXT_XPAD, 
  132.                              fm.getMaxAscent() + fm.getMaxDescent() + TEXT_YPAD);
  133.     }
  134.  
  135.     public Dimension getMinimumSize() {
  136.         return getPreferredSize();
  137.     }
  138.  
  139.     private void sizeToFit() {
  140.         Dimension d = getPreferredSize();
  141.         setSize(d.width, d.height);
  142.         Component p = getParent();
  143.         if (p != null) {
  144.             p.invalidate();
  145.             p.doLayout();
  146.         }
  147.     }
  148.  
  149.     public boolean handleEvent(Event evt) {
  150.         if (! isEnabled()) {
  151.             return false;
  152.         }
  153.         switch (evt.id) {
  154.         case Event.MOUSE_DOWN:
  155.             down = true;
  156.             repaint();
  157.             return true;
  158.         case Event.MOUSE_UP:
  159.             if (down) {
  160.                 fireAction();
  161.                 down = false;
  162.                 repaint();
  163.             }
  164.             return true;
  165.         }
  166.         return false;
  167.     }
  168.  
  169.     public synchronized void addActionListener(ActionListener l) {
  170.         listeners.addElement(l);
  171.     }
  172.  
  173.     public synchronized void removeActionListener(ActionListener l) {
  174.         listeners.removeElement(l);
  175.     }
  176.  
  177.     public void addPropertyChangeListener(PropertyChangeListener l) {
  178.         changes.addPropertyChangeListener(l);
  179.     }
  180.  
  181.     public void removePropertyChangeListener(PropertyChangeListener l) {
  182.         changes.removePropertyChangeListener(l);
  183.     }
  184.  
  185.     public void incrementCount(ActionEvent x) {
  186.         count++;
  187.         updateLabel();
  188.         sizeToFit();
  189.     }
  190.  
  191.     public void decrementCount(ActionEvent x) {
  192.         count--;
  193.         updateLabel();
  194.         sizeToFit();
  195.     }
  196.  
  197.  
  198.   // temp, called by TestAcme09Bean.java
  199.     public void incrementCount() {
  200.         count++;
  201.         updateLabel();
  202.         sizeToFit();
  203.         repaint();
  204.     }
  205.  
  206.   // temp, called by TestAcme09Bean.java
  207.     public void decrementCount() {
  208.         count--;
  209.         updateLabel();
  210.         sizeToFit();
  211.         repaint();
  212.     }
  213.  
  214.  
  215.     private int count = 1;
  216.     private boolean debug = true;
  217.     private Color beanColor = Color.cyan;
  218.     private PropertyChangeSupport changes = new PropertyChangeSupport(this);
  219.     private Vector listeners = new Vector();
  220.     private String label;
  221.     private boolean down;
  222.     static final int TEXT_XPAD = 12;
  223.     static final int TEXT_YPAD = 8;
  224. }
  225.